#!/usr/bin/env bash
# =====================================
# 固定Mac地址
sudo mkdir -p /etc/NetworkManager/conf.d/
MAC_FILE="/etc/NetworkManager/conf.d/100-disable-wifi-mac-randomization.conf"
if [ -e "$MAC_FILE" ]; then
    echo "$MAC_FILE exists."
else
    echo "$MAC_FILE does not exist."
    touch $MAC_FILE
fi

cat << EOF > /etc/NetworkManager/conf.d/100-disable-wifi-mac-randomization.conf
[connection]
wifi.mac-address-randomization=1

[device]
wifi.scan-rand-mac-address=no

EOF

# =====================================
# 安装Network-Manager
sudo apt install -y network-manager
# 禁用ModemManager
sudo systemctl stop ModemManager
sudo systemctl disable ModemManager

# =====================================
# 安装Apparmor
sudo apt install -y apparmor-utils jq software-properties-common apt-transport-https avahi-daemon ca-certificates curl dbus socat


if [ -e "/boot/cmdline.txt" ]; then
    echo "/boot/cmdline.txt exists."
else
    echo "/boot/cmdline.txt does not exist."
    touch /boot/cmdline.txt
fi


cat << EOF > /boot/cmdline.txt
apparmor=1
security=apparmor

EOF

# =====================================
## 安装 
OS_AGENT=$(curl -s "https://api.github.com/repos/home-assistant/os-agent/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
wget https://github.com/home-assistant/os-agent/releases/download/"${OS_AGENT}"/os-agent_"${OS_AGENT}"_linux_aarch64.deb
sudo dpkg -i os-agent_"${OS_AGENT}"_linux_aarch64.deb
## 安装其他依赖
sudo apt -y install jq wget curl udisks2 libglib2.0-bin dbus

# =====================================
## 安装 Docker
sh -c "$(curl -fsSL https://gitee.com/jonnyhub/raspi-config/raw/master/tools/docker_install.sh)"

## 处理可能存在的报错问题
echo -e "\nsystemd.unified_cgroup_hierarchy=false" >> /etc/default/grub
echo -e "\nsystemd.unified_cgroup_hierarchy=false" >> /boot/cmdline.txt

## 重启设备
reboot












